package com.whq.knowverywell.adapter;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.SparseArray;

import com.actionbarsherlock.app.SherlockFragment;
import com.whq.knowverywell.R;
import com.whq.knowverywell.activity.ContentFragment;
import com.whq.knowverywell.util.LogUtil;
import com.whq.knowverywell.util.constant.ModelConstant;

public class ContentFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
	private static final String TAG = ContentFragmentAdapter.class.getSimpleName();
	
    protected static final String[] CONTENT = new String[] { "昨天", "今天", "当月", "当年", };
    protected static final int[] durationTypes = new int[]{ModelConstant.DURATION_TYPE_YESTERDAY, ModelConstant.DURATION_TYPE_TODAY, ModelConstant.DURATION_TYPE_MONTH, ModelConstant.DURATION_TYPE_YEAR};
    protected static final int[] ICONS = new int[] {
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher,
            R.drawable.ic_launcher
    };

    private int mCount = CONTENT.length;
    private int fragmentType = ModelConstant.FRAGEMENT_TYPE_CONTENT;
    private int businessType = ModelConstant.BUSINESS_TYPE_SUMMARY;
    
    private SparseArray<ContentFragment> fragments = new SparseArray<ContentFragment>();

    public ContentFragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public ContentFragment getItem(int position) {
    	LogUtil.d(TAG, "getItem position[" + position + "] businessType[" + businessType + "]");
    	int key = position % durationTypes.length;
    	ContentFragment fragment = fragments.get(key);
    	if (fragment == null) {
    	    fragment = ContentFragment.newInstance(durationTypes[position % durationTypes.length], businessType, fragmentType);
    	    fragments.put(key, fragment);
    	    return fragment;
    	} 
    	return fragment;
    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public CharSequence getPageTitle(int position) {
      return ContentFragmentAdapter.CONTENT[position % CONTENT.length];
    }

    @Override
    public int getIconResId(int index) {
      return ICONS[index % ICONS.length];
    }

    public void setCount(int count) {
        if (count > 0 && count <= 10) {
            mCount = count;
            notifyDataSetChanged();
        }
    }

	public int getFragmentType() {
		return fragmentType;
	}

	public void setFragmentType(int fragmentType) {
		this.fragmentType = fragmentType;
	}

	public int getBusinessType() {
		return businessType;
	}

	public void setBusinessType(int businessType) {
		this.businessType = businessType;
	}
}